<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
            <el-form-item label="表单名称" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入表单名称" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="表单分类" prop="category">
                <el-select v-model="queryParams.category" placeholder="请选择表单分类" clearable>
                    <el-option v-for="dict in dict.type.oa_customform_category" :key="dict.value" :label="dict.label"
                               :value="dict.value"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <TableCard>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                               v-hasPermi="['oa:customForm:add']">新增
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                               v-hasPermi="['oa:customForm:export']">导出
                    </el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>

            <el-table  :border="Global.tableShowBorder" v-loading="loading" :data="customFormList" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"/>
                <!--      <el-table-column label="主键" align="center" prop="id" />-->
                <el-table-column label="表单代码" align="center" prop="formKey" />
                <el-table-column label="表单名称" align="center" prop="name"/>
                <el-table-column label="表单分类" align="center" prop="category">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.oa_customform_category" :value="scope.row.category"/>
                    </template>
                </el-table-column>
                <el-table-column label="备注" align="center" prop="remark"/>
                <el-table-column label="绑定流程" align="center" prop="flowKey" width="200">
                    <template slot-scope="scope">
                        {{flowKeyToName(scope.row.flowKey)}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="350">
                    <template slot-scope="scope">
                        <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                                   v-hasPermi="['oa:customForm:edit']">修改
                        </el-button>
                        <el-button size="mini" type="text" icon="el-icon-edit" @click="openFormDesigner(scope.row)"
                                   v-hasPermi="['oa:customForm:formDesigner']">设计表单
                        </el-button>
                        <el-button size="mini" type="text" icon="el-icon-edit" @click="showBindDialod(scope.row)" style="color: #ff6666"
                                   v-hasPermi="['oa:customForm:edit']">绑定流程
                        </el-button>
                        <el-button size="mini" type="text" icon="el-icon-edit" @click="previewForm(scope.row)"
                                   v-hasPermi="['oa:customForm:formDesigner']">预览
                        </el-button>
                        <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                                   v-hasPermi="['oa:customForm:remove']">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                        @pagination="getList"/>
        </TableCard>
        <!-- 添加或修改自定义表单对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="表单代码" prop="formKey">
                    <el-input v-model="form.formKey" placeholder="请输入表单代码" />
                </el-form-item>
                <el-form-item label="表单名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入表单名称" />
                </el-form-item>
                <el-form-item label="表单分类" prop="category">
                    <el-select v-model="form.category" placeholder="请选择表单分类" clearable>
                        <el-option v-for="dict in dict.type.oa_customform_category" :key="dict.value" :label="dict.label"
                                   :value="dict.value"/>
                    </el-select>
                </el-form-item>


                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 预览对话框 -->
        <el-dialog :title="title" :visible.sync="openPreview" width="1000px" append-to-body>
            <form-create :rule="preview.rules" :option="preview.options"></form-create>
        </el-dialog>

        <el-dialog title="绑定流程"  :visible.sync="showBindFlowKey" width="400px" append-to-body>
            <el-form size="small" :inline="true" label-width="68px">
                <el-form-item label="绑定流程" prop="flowKey">
                    <el-select v-model="flowKey" placeholder="请选择要绑定的流程" clearable>
                        <el-option v-for="f in flowList" :key="f.key" :label="f.name" :value="f.key"/>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="bindFlowKey">确 定</el-button>
                <el-button @click="showBindFlowKey=false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {listCustomForm, getCustomForm, delCustomForm, addCustomForm, updateCustomForm} from "@/api/oa/customForm";
import {getAllFlowList} from "@/api/workflow/definition";
export default {
    name: "CustomForm",
    dicts: ['oa_customform_category'],
    data() {
        return {
            //绑定流程
            flowList:[],
            flowKey:"",
            showBindFlowKey:false,
            selectBindRowId:null,

            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 自定义单表格数据
            customFormList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                name: null,
                category: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            // 预览
            openPreview:false,
            preview:{},
        };
    },
    created() {
        this.getList();
        this.getAllFlowLists();
    },
    methods: {
        /** 查询自定义单列表 */
        getList() {
            this.loading = true;
            listCustomForm(this.queryParams).then(response => {
                this.customFormList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                name: null,
                category: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加自定义单";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getCustomForm(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改自定义单";
            });
        },
        showBindDialod(row){
            this.showBindFlowKey = true;
            this.selectBindRowId = row.id;
            this.flowKey = row.flowKey;
        },
        bindFlowKey(){
            let f = {};
            f.id = this.selectBindRowId;
            f.flowKey = this.flowKey;
            if(!this.flowKey){
                this.$modal.msgWarning("请选择自定义表单绑定流程");
                return;
            }
            updateCustomForm(f).then(response => {
                this.$modal.msgSuccess("绑定成功");
                this.showBindFlowKey = false;
                this.getList();
            });
        },
        /** 打开表单设计器 */
        openFormDesigner(row) {
            this.$router.push({path: "/oa/customForm/formDesigner/" + row.id});
        },
        /** 预览 */
        previewForm(row){
            let rules = "";
            let options = "";
            if(row.rules){
                rules = JSON.parse(row.rules);
            }
            if(row.options){
                options = JSON.parse(row.options);
            }
            this.preview.rules = rules;
            this.preview.options = options;
            this.openPreview = true;
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateCustomForm(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addCustomForm(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除自定义单编号为"' + ids + '"的数据项？').then(function () {
                return delCustomForm(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('oa/customForm/export', {
                ...this.queryParams
            }, `customForm_${new Date().getTime()}.xlsx`)
        },
        getAllFlowLists(ifUpdate){
            getAllFlowList().then(res=>{
                if(200==res.code){
                    this.flowList = res.data;
                }
            })
        },
        flowKeyToName(key){
            let name = "";
            this.flowList.forEach(item=>{
                if(item.key == key){
                    name = item.name;
                }
            })
            return name;
        }
    }
};
</script>


